#include "glwidget.h"
#include "GL/glut.h"

#include <QtCore/QCoreApplication>

#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLPaintDevice>
#include <QtGui/QPainter>

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent), m_program(0)
{
    connect(&timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()));
    timer.start(16);
}

GLWidget::~GLWidget()
{
}

void GLWidget::initialize()
{
}

void GLWidget::render()
{

}

void GLWidget::update()
{

}

void GLWidget::initializeGL()
{
    glClearColor(0.4, 0.58, 0.94, 1);
}

void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    render();
}

void GLWidget::resizeGL(int w, int h)
{
    glViewport( 0, 0, (GLint)w, (GLint)h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glFrustum( 0, 0, 0, 0, -1.0, 15.0 );
    glMatrixMode( GL_MODELVIEW );
}

void GLWidget::timeOutSlot()
{
    update();
}

QVector2D GLWidget::PixelsToWorldPoint(int x, int y)
{
    return QVector2D();
}

QVector2D GLWidget::WorldToPixelsPoint(float x, float y)
{
    return QVector2D(width() * (x + 1.0)/2.0 , height() * (y + 1.0)/2.0);
}

QVector2D GLWidget::PixelsToWorldLength(int dx)
{
    return QVector2D((float)((float)dx / width()), (float)((float)dx / height()));
}

QVector2D GLWidget::WorldToPixelsLength(float dx)
{
    return QVector2D(dx  * width(), dx * height());
}
